//
//  LEOrderFinishedController.m
//  LoveExpressBusiness
//
//  Created by 刘甲奇 on 2017/5/17.
//  Copyright © 2017年 DFH. All rights reserved.
//

#import "LEOrderFinishedController.h"
#import "LEOrderCell.h"
#import "LEOderModel.h"

#import "LEOrderDetialController.h"
#import "LEOrderCenterController.h"

static NSString *const LEOrderCellId = @"LEOrderCell";

@interface LEOrderFinishedController ()

@end

@implementation LEOrderFinishedController

- (void)setupUI
{
    [super setupUI];
    [self.tableView registerNib:[UINib nibWithNibName:@"LEOrderCell" bundle:nil] forCellReuseIdentifier:LEOrderCellId];
    [self resetTablewFrame:CGRectMake(0, 0, LEScreenWidth, LEScreenHeight-40-64 )];
    
    [self initHeader];
    [self initFooter];
    
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.tabBarController.tabBar setHidden:YES];
}
- (void)loadData
{
    self.page = 1;
    [self requestRefreshData];
}

#pragma mark- 网络请求
- (void)requestRefreshData
{
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setObject:@(self.page) forKey:@"curpage"];
    [dict setObject:@"10" forKey:@"page"];
    [dict setObject:@"3" forKey:@"flag"];
    LEOrderCenterController *parentVC = (LEOrderCenterController *)self.parentViewController;
    if (parentVC.orderMode == TodayOrder) {
        [dict setValue:@"day" forKey:@"time"];
    }else{
        
    }
    
    [SVProgressHUD showWithStatus:@"正在加载中"];
     __weak typeof (self) weakSelf = self;
    [API requestAppendKey:StoreOrderCenterUrl params:dict success:^(NSDictionary *successCode) {
        [SVProgressHUD dismiss];
        NSDictionary *datas = successCode[@"datas"];
        NSString *error = datas[@"error"];
        NSString *totalPage = successCode[@"page_total"];
        weakSelf.totalPage = totalPage;
        if (error)
        {
            [weakSelf addNoDataViewWithTipText:error];
            [weakSelf.dataSource removeAllObjects];

        }else
        {
            [weakSelf hiddenNoDataView];
            
            NSDictionary *orderList = datas[@"order_list"];
            NSArray *tempt = [orderList toArray:orderList];
            NSArray *tempModel = [LEOderModel mj_objectArrayWithKeyValuesArray:tempt];
            
            if (weakSelf.page == 1) {
                [weakSelf.dataSource removeAllObjects];
                [weakSelf.dataSource addObjectsFromArray:tempModel];
                
            }else{
                [weakSelf.dataSource addObjectsFromArray:tempModel];
            }
        }
        [weakSelf.tableView reloadData];
        LELog(@"%s---%d---%@",__func__,__LINE__,successCode);
    } falure:^(NSError *er) {
        LELog(@"%s---%d---%@",__func__,__LINE__,er);
        [SVProgressHUD dismiss];
    }];
}

#pragma mark- tableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSource.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    LEOrderCell *cell = [tableView dequeueReusableCellWithIdentifier:LEOrderCellId forIndexPath:indexPath];
    if (self.dataSource.count>0) {
        LEOderModel *ordeModel = self.dataSource[indexPath.row];
        cell.model = ordeModel;
    }
    
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    LEOrderDetialController *ordeDetial = [[LEOrderDetialController alloc] init];
    LEOderModel *orderModel = self.dataSource[indexPath.row];
    ordeDetial.orderModel = orderModel;
    [self.navigationController pushViewController:ordeDetial animated:YES];
}


@end
